package com.example.chiro;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Table_espece {

	public static final String ROW_ID = "_id";

	public static final String ID_FICHE = "id_fiche";
	public static final String PATH_PHOTO = "path_photo";
	public static final String NOM = "nom";
	public static final String EFFECTIF = "effectif";
	public static final String RARETE = "rarete";
	public static final String COMMENTAIRE = "commentaire";
	public static final String ENVOYE = "envoye";

	public static final String DATABASE_TABLE = "espece";
	private DataBaseHelper DBHelper;
	private SQLiteDatabase DB;
	private final Context context;

	private static class DataBaseHelper extends SQLiteOpenHelper {
		public DataBaseHelper(Context context) {
			super(context, DB_FICHEARBRE_adapter.DATABASE_NAME, null,
					DB_FICHEARBRE_adapter.DATABASE_VERSION);
		}

		public void onCreate(SQLiteDatabase arg0) {
		}

		public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		}
	}

	public Table_espece(Context c) {
		this.context = c;
	}

	public Table_espece open() throws SQLException {
		this.DBHelper = new DataBaseHelper(this.context);
		this.DB = this.DBHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		this.DBHelper.close();
	}

	public boolean delete_donnees_general(long rowId) {
		return this.DB.delete(DATABASE_TABLE, ROW_ID + "=" + rowId, null) > 0;
	}

	public boolean update_envoi(int rowId, String etat) {
	    ContentValues args = new ContentValues();
	    args.put(ENVOYE, etat);
	    return this.DB.update(DATABASE_TABLE, args, ROW_ID + "=" + rowId, null) > 0;
	}

	public void inserer_parSlide(ContentValues[] recolte_donnees) {
		// TODO Auto-generated method stub
		for(int i=0;i<recolte_donnees.length;i++)
			this.DB.insert(DATABASE_TABLE, null, recolte_donnees[i]);
	}

	public Cursor getByID(int i) {
		Cursor resultat = null;
		resultat = this.DB.query(DATABASE_TABLE, new String[]{ROW_ID,ID_FICHE,PATH_PHOTO,NOM,EFFECTIF,RARETE,COMMENTAIRE,ENVOYE}, ID_FICHE+"=\""+String.valueOf(i)+"\"",null,null,null,null );
		return resultat;
	}
	
	public int nombre_non_envoye() {
		int c = DB.query(DATABASE_TABLE, new String[] { "_id" }, ENVOYE + "=" + "false", null,
				null, null, null).getCount();
		return c;
	}
}
